package com.huawang.mdesign.model.service.util.uaf;

import com.huawang.business.dto.ProjectDTO;
import com.huawang.business.dto.project.ModelDependency;
import com.huawang.business.dto.project.Project;
import com.huawang.core.semantics.constants.ModelPackageNameConstants;
import com.huawang.mdesign.model.service.constant.ProjectSpecificationEnum;
import com.huawang.mdesign.model.service.constant.UAFProfileConstants;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

import static com.huawang.business.constants.ProfileConstants.DEFAULT_PROFILE;

/**
 * 项目工具类
 */
public class ProjectUtils {
    private ProjectUtils(){}

    /**
     * 根据项目的规范定义全局的项目规范范围DEFAULT_PROFILE
     * @param projectSpecification
     */
    public static void updateProfileByProjectSpecification(String projectSpecification) {
        if(ProjectSpecificationEnum.DODAF.value().equals(projectSpecification) ||
                ProjectSpecificationEnum.UAF.value().equals(projectSpecification) ||
                ProjectSpecificationEnum.MISAF.value().equals(projectSpecification) ||
                ProjectSpecificationEnum.WLXXTX.value().equals(projectSpecification) ||
                ProjectSpecificationEnum.TKZZTX.value().equals(projectSpecification) ||
                ProjectSpecificationEnum.UPDM.value().equals(projectSpecification)) {
            if(!DEFAULT_PROFILE.contains(ProjectSpecificationEnum.UAF.value())) {
                DEFAULT_PROFILE.add(ModelPackageNameConstants.UAF);
                DEFAULT_PROFILE.add(UAFProfileConstants.UAF_EXTRA);
            }
        } else {
            if(DEFAULT_PROFILE.contains(ProjectSpecificationEnum.UAF.value())) {
                DEFAULT_PROFILE.remove(ModelPackageNameConstants.UAF);
                DEFAULT_PROFILE.remove(UAFProfileConstants.UAF_EXTRA);
            }
        }
    }

    /**
     * 回滚默认profile
     * @param oldDefaultProfile
     */
    public static void rollBackDefaultProfile(List<String> oldDefaultProfile) {
        DEFAULT_PROFILE.clear();
        DEFAULT_PROFILE.addAll(oldDefaultProfile);
    }

    /**
     * 通过ProjectDTO获取项目的规范
     * @param projectDTO
     * @return
     */
    public static String getProjectSpecificationByProjectDTO(@NotNull ProjectDTO projectDTO) {
        return Optional.ofNullable(projectDTO)
                .filter(e -> StringUtils.isBlank(e.getSpecification()))
                .map(ProjectDTO::getDependencies)
                .filter(CollectionUtils::isNotEmpty)
                .filter(k ->
                        k.stream()
                                .map(ModelDependency::getProfileName)
                                .filter(StringUtils::isNotBlank)
                                .anyMatch(e -> Objects.equals(e, ProjectSpecificationEnum.UAF.value()) ||
                                        Objects.equals(e, UAFProfileConstants.UAF_EXTRA)
                                )
                ).map(e -> ProjectSpecificationEnum.UAF.value())
                .orElse(Objects.nonNull(projectDTO) ? projectDTO.getSpecification() : null);
    }

    public static boolean isSysmlProject(Project project) {
        return project.getProjectSpecification() == null || Objects.equals(project.getProjectSpecification(),
                ProjectSpecificationEnum.SYSMML.value());
    }
}
